import Axios from 'axios'
import qs from 'qs'
import { Message, Loading } from 'element-ui'
// class 
const http = Axios.create({
    baseURL: '/api',
    headers: {
    'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
    }
})
http.interceptors.response.use(
    response =>{
        return response.data;
    },
    error =>{
        if(error.response.status == 500){
            Message.error({message:'接口错误 <br />', dangerouslyUseHTMLString:true,duration :0})
        }else if(error.response.status == 404){
            Message.error({
                message:'找不到数据 <br /><br /> NotFoundHttpException',
                dangerouslyUseHTMLString:true,
                duration :0
            })
        }
        console.log(error.response.data)
        return error;
    }
)

var instance = Axios.create({
    headers: {'content-type':'multipart/form-data'}
});


/**
 * 封装get方法
 * @param url
 * @param data
 * @returns {Promise}
 */
export function get(url,params={}){
    params.token = sessionStorage.getItem('accessToken')
    return new Promise((resolve,reject) => {
        Axios.get(url,{
        params:params
      })
      .then(response => {
        resolve(response.data);
      })
      .catch(err => {
        reject(err)
      })
    })
  }
  
    /**
   * 封装post请求
   * @param url
   * @param data
   * @returns {Promise}
   */
  
  export function post(url,data = {}){
    data.token = sessionStorage.getItem('accessToken')
    return new Promise((resolve,reject) => {
        Axios.post(url,data)
      .then(response => {
        resolve(response.data);
      },err => {
        reject(err)
      })
    })
  }
  
    /**
   * 封装post请求 FormData方式
   * @param url
   * @param data
   * @returns {Promise}
   */
  
  export function postform(url,data = {}){
    data.token = sessionStorage.getItem('accessToken')
    return new Promise((resolve,reject) => {
      instance.post(url,data)
      .then(response => {
        resolve(response.data);
      },err => {
        reject(err)
      })
    })
  }
  